package com.health.pay.tencent.protocol.unifiedorder;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.util.TextUtils;

import com.health.pay.tencent.common.Configure;
import com.health.pay.tencent.common.Signature;
import com.health.pay.tencent.protocol.BaseReqData;
import com.sun.istack.internal.NotNull;

public class UnifiedorderReqData extends BaseReqData {

	// 每个字段具体的意思请查看API文档
	private String appid;
	private String mch_id;
	private String device_info;
	private String nonce_str;
	private String sign_type;
	private String body;
	private String detail;
	private String attach;
	private String out_trade_no;
	private String fee_type;
	private String total_fee;
	private String spbill_create_ip;
	private String time_start;
	private String time_expire;
	private String goods_tag;
	private String notify_url;
	private String trade_type;
	private String limit_pay;
	private String openid;
	private String scene_info;
	private String sign;

	/**
	 * @param deviceInfo
	 *            设备号 终端设备号(门店号或收银设备ID)，默认请传"WEB"(可选)
	 * @param nonceStr
	 *            随机字符串，不长于32位(必传)
	 * @param signType
	 *            签名类型，目前支持HMAC-SHA256和MD5，默认为MD5(可选)
	 * @param body
	 *            商品描述交易字段格式根据不同的应用场景按照以下格式：APP——需传入应用市场上的APP名字-实际商品名称，天天爱消除-游戏充值
	 *            。(必传)
	 * @param detail
	 *            商品详细列表，使用Json格式，传输签名前请务必使用CDATA标签将JSON文本串保护起来(可选)
	 * @param attach
	 *            附加数据，在查询API和支付通知中原样返回，该字段主要用于商户携带订单的自定义数据(可选)
	 * @param outTradeNo
	 *            商户系统内部的订单号,32个字符内、可包含字母(必传)
	 * @param totalFee
	 *            订单总金额，单位为分(必传)
	 * @param spbillCreateIp
	 *            用户端实际ip(必传)
	 * @param timeStart
	 *            订单生成时间，格式为yyyyMMddHHmmss(可选)
	 * @param timeExpire
	 *            订单失效时间，格式为yyyyMMddHHmmss(可选)
	 * @param notifyUrl
	 *            接收微信支付异步通知回调地址(必传)
	 * @param limitPay
	 *            指定支付方式(可选)
	 * */
	public UnifiedorderReqData(String deviceInfo, @NotNull String nonceStr,
			String signType, @NotNull String body, String detail,
			String attach, @NotNull String outTradeNo,
			@NotNull String totalFee, @NotNull String spbillCreateIp,
			String timeStart, String timeExpire, @NotNull String notifyUrl,
			String limitPay) {

		super();
		setAppid(Configure.getAppid());
		setMch_id(Configure.getMchid());
		setDevice_info(deviceInfo);
		setNonce_str(nonceStr);
		setSign_type(signType);
		setBody(body);
		setDetail(detail);
		setAttach(attach);
		setOut_trade_no(outTradeNo);
		setTotal_fee(totalFee);
		setSpbill_create_ip(spbillCreateIp);
		setTime_start(timeStart);
		setTime_expire(timeExpire);
		setNotify_url(notifyUrl);
		setTrade_type("APP");
		setLimit_pay(limitPay);
		
        //根据API给的签名规则进行签名
        String sign = Signature.getSign(toMap());
        setSign(sign);//把签名数据设置到Sign这个属性中
	}

	/**
	 * 微信H5调用此构造函数
	 * */
	public UnifiedorderReqData(String deviceInfo, @NotNull String nonceStr,
							   String signType, @NotNull String body, String detail,
							   String attach, @NotNull String outTradeNo,
							   @NotNull String totalFee, @NotNull String spbillCreateIp,
							   String timeStart, String timeExpire, @NotNull String notifyUrl,String tradeType,
							   String limitPay,String sceneInfo) {
		super();
		setAppid(Configure.getAppid());
		setMch_id(Configure.getMchid());
		setDevice_info(deviceInfo);
		setNonce_str(nonceStr);
		setSign_type(signType);
		setBody(body);
		setDetail(detail);
		setAttach(attach);
		setOut_trade_no(outTradeNo);
		setTotal_fee(totalFee);
		setSpbill_create_ip(spbillCreateIp);
		setTime_start(timeStart);
		setTime_expire(timeExpire);
		setNotify_url(notifyUrl);
		setTrade_type(tradeType);
		setLimit_pay(limitPay);
		setScene_info(sceneInfo);

		//根据API给的签名规则进行签名
		String sign = Signature.getSign(toMap());
		setSign(sign);//把签名数据设置到Sign这个属性中

	}

	
	
	/**
	 * 微信H5调用此构造函数
	 * */
	public UnifiedorderReqData(String deviceInfo, @NotNull String nonceStr,
							   String signType, @NotNull String body, String detail,
							   String attach, @NotNull String outTradeNo,
							   @NotNull String totalFee, @NotNull String spbillCreateIp,
							   String timeStart, String timeExpire, @NotNull String notifyUrl,String tradeType,
							   String limitPay,String openid,String extend1,String extend2) {
		super();
		setAppid(Configure.getMiNiAppID());
		setMch_id(Configure.getMiNiMchID());
		setDevice_info(deviceInfo);
		setNonce_str(nonceStr);
		setSign_type(signType);
		setBody(body);
		setDetail(detail);
		setAttach(attach);
		setOut_trade_no(outTradeNo);
		setTotal_fee(totalFee);
		setSpbill_create_ip(spbillCreateIp);
		setTime_start(timeStart);
		setTime_expire(timeExpire);
		setNotify_url(notifyUrl);
		setTrade_type(tradeType);
		setLimit_pay(limitPay);
		setOpenid(openid);

		//根据API给的签名规则进行签名
		String sign = Signature.getSign(toMap());
		setSign(sign);//把签名数据设置到Sign这个属性中

	}
	
	/**
	 * 微信公众调用此函数
	 * */
	public UnifiedorderReqData(String deviceInfo, @NotNull String nonceStr,
							   String signType, @NotNull String body, String detail,
							   String attach, @NotNull String outTradeNo,
							   @NotNull String totalFee, @NotNull String spbillCreateIp,
							   String timeStart, String timeExpire, @NotNull String notifyUrl,String tradeType,
							   String limitPay,String sceneInfo,String openid) {
		super();
		setAppid(Configure.getMpAppID());
		setMch_id(Configure.getMpMchID());
		setDevice_info(deviceInfo);
		setNonce_str(nonceStr);
		setSign_type(signType);
		setBody(body);
		setDetail(detail);
		setAttach(attach);
		setOut_trade_no(outTradeNo);
		setTotal_fee(totalFee);
		setSpbill_create_ip(spbillCreateIp);
		setTime_start(timeStart);
		setTime_expire(timeExpire);
		setNotify_url(notifyUrl);
		setTrade_type(tradeType);
		setLimit_pay(limitPay);
		setScene_info(sceneInfo);
		setOpenid(openid);

		//根据API给的签名规则进行签名
		String sign = Signature.getSign(toMap());
		setSign(sign);//把签名数据设置到Sign这个属性中

	}

	
	public void setAppid(String appid) {
		if (!TextUtils.isEmpty(appid)) {
			this.appid = appid;
		}
	}

	public void setMch_id(String mchId) {
		if (!TextUtils.isEmpty(mchId)) {
			this.mch_id = mchId;
		}
	}

	public void setDevice_info(String deviceInfo) {
		if (!TextUtils.isEmpty(deviceInfo)) {
			this.device_info = deviceInfo;
		}
	}

	public void setNonce_str(String nonceStr) {
		if (!TextUtils.isEmpty(nonceStr)) {
			this.nonce_str = nonceStr;
		}
	}

	public void setSign_type(String signType) {
		if (!TextUtils.isEmpty(signType)) {
			this.sign_type = signType;
		}
	}

	public void setBody(String body) {
		if (!TextUtils.isEmpty(body)) {
			this.body = body;
		}
	}

	public void setDetail(String detail) {
		if (!TextUtils.isEmpty(detail)) {
			this.detail = detail;
		}
	}

	public void setAttach(String attach) {
		if (!TextUtils.isEmpty(attach)) {
			this.attach = attach;
		}
	}

	public void setOut_trade_no(String outTradeNo) {
		if (!TextUtils.isEmpty(outTradeNo)) {
			this.out_trade_no = outTradeNo;
		}
	}

	public void setFee_type(String feeType) {
		if (!TextUtils.isEmpty(feeType)) {
			this.fee_type = feeType;
		}
	}

	public void setTotal_fee(String totalFee) {
		if (!TextUtils.isEmpty(totalFee)) {
			this.total_fee = totalFee;
		}
	}

	public void setSpbill_create_ip(String spbillCreateIp) {
		if (!TextUtils.isEmpty(spbillCreateIp)) {
			this.spbill_create_ip = spbillCreateIp;
		}
	}

	public void setTime_start(String timeStart) {
		if (!TextUtils.isEmpty(timeStart)) {
			this.time_start = timeStart;
		}
	}

	public void setTime_expire(String timeExpire) {
		if (!TextUtils.isEmpty(timeExpire)) {
			this.time_expire = timeExpire;
		}
	}

	public void setGoods_tag(String goodsTag) {
		if (!TextUtils.isEmpty(goodsTag)) {
			this.goods_tag = goodsTag;
		}
	}

	public void setNotify_url(String notifyUrl) {
		if (!TextUtils.isEmpty(notifyUrl)) {
			this.notify_url = notifyUrl;
		}
	}

	public void setTrade_type(String tradeType) {
		if (!TextUtils.isEmpty(tradeType)) {
			this.trade_type = tradeType;
		}
	}

	public void setLimit_pay(String limitPay) {
		if (!TextUtils.isEmpty(limitPay)) {
			this.limit_pay = limitPay;
		}
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		Field[] fields = this.getClass().getDeclaredFields();
		for (Field field : fields) {
			Object obj;
			try {
				obj = field.get(this);
				if (obj != null) {
					map.put(field.getName(), obj);
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

	public String getAppid() {
		return appid;
	}

	public String getMch_id() {
		return mch_id;
	}

	public String getDevice_info() {
		return device_info;
	}

	public String getNonce_str() {
		return nonce_str;
	}

	public String getSign_type() {
		return sign_type;
	}

	public String getBody() {
		return body;
	}

	public String getDetail() {
		return detail;
	}

	public String getAttach() {
		return attach;
	}

	public String getOut_trade_no() {
		return out_trade_no;
	}

	public String getFee_type() {
		return fee_type;
	}

	public String getTotal_fee() {
		return total_fee;
	}

	public String getSpbill_create_ip() {
		return spbill_create_ip;
	}

	public String getTime_start() {
		return time_start;
	}

	public String getTime_expire() {
		return time_expire;
	}

	public String getGoods_tag() {
		return goods_tag;
	}

	public String getNotify_url() {
		return notify_url;
	}

	public String getTrade_type() {
		return trade_type;
	}

	public String getLimit_pay() {
		return limit_pay;
	}

	public String getSign() {
		return sign;
	}

	public String getScene_info() {
		return scene_info;
	}

	public void setScene_info(String scene_info) {
		this.scene_info = scene_info;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}
}
